import { secondCtgyModel } from './SecCtgyModel'
import { thirdCtgyModel } from './ThirdCtgyModel'

//one to many
secondCtgyModel.hasMany(thirdCtgyModel, {
  as: 'thirdCtgy',
  foreignKey: 'secctgyid'
})

//many to one
thirdCtgyModel.belongsTo(secondCtgyModel, {
  // as: 'secondCtgy',
  foreignKey: 'secctgyid',
  targetKey: 'secondctgyid'
})

export const findSecThrdCtgysByFstCtgyId = async (firstCtgyId: number) => {
  const result = await secondCtgyModel.findAll({
    // raw: true,
    where: {
      firstctgyid: firstCtgyId
    },
    include: [
      {
        model: thirdCtgyModel,
        as: 'thirdCtgy'
      }
    ]
  })
  return result
  // console.log(result)
}

// findSecThrdCtgysByFstCtgyId(1)
